import { useCallback, useState } from 'react'

const useAsyncFn = (fn) => {
  const [state, set] = useState<any>({ loading: false })
  const handleAsyncFn = useCallback(
    (...args) => {
      set({ loading: true })
      console.log(...args)
      fn(args)
        .then((value) => {
          set({ loading: false, value })
        })
        .catch((error) => {
          set({ loading: false, error })
        })
    },
    [fn],
  )
  return [state, handleAsyncFn]
}
export default useAsyncFn
